/*
 * @Description:
 * @Author: cdtangxiejun
 * @version:
 * @Date: 2024-08-28 20:53:50
 * @LastEditors: cdtangxiejun
 * @LastEditTime: 2024-09-03 20:49:55
 */

const path = require('path')

/**
 * 查找真实的包路径 主要针对pnpm这种node_modules包管理器导致的软连接路径不是常规的情况
 * 参考：https://juejin.cn/post/7127584537030164487
 * @param {*} id 包名
 * @param {*} searchValue 需要被替换的值
 * @param {*} replaceValue 替换的值
 * @returns
 */
const resolvePath = (id, searchValue = '', replaceValue = '') => {
  const fullPath = require.resolve(id)
  if (!searchValue) {
    return fullPath
  }
  const relativePath =
    fullPath &&
    fullPath.replace(path.normalize(searchValue), path.normalize(replaceValue))
  return relativePath
}

module.exports = {
  resolvePath,
}
